package com.service;

import com.domain.OrderGood;
import com.domain.OrderMall;
import com.domain.result.ResultData;
import com.github.pagehelper.PageHelper;
import com.mall.RandomUtil;
import com.mapper.primary.BunsMapper;
import com.mapper.primary.OrderGoodMapper;
import com.mapper.primary.OrderMallMapper;
import com.service.seinterface.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    OrderMallMapper orderMallMapper;

    @Resource
    OrderGoodMapper orderGoodMapper;

    @Resource
    BunsMapper bunsMapper;

    @Transactional
    @Override
    public ResultData addOrder(OrderMall orderMall, List<OrderGood> orderGoods) {
        ResultData resultData=new ResultData();
        orderMall.setOrderno(RandomUtil.getRandomNo());
        orderMall.setState(1);
        try {
            orderMallMapper.insert(orderMall);
            for(OrderGood orderGood:orderGoods){
                orderGood.setOrdid(orderMall.getOrderid());
                bunsMapper.reduceByBunsIdAndNum(orderGood.getGoodid(),orderGood.getGoodnum());
                orderGoodMapper.insert(orderGood);
            }
            resultData.setMessage("添加成功");
        } catch (Exception e) {
            resultData.setResult(1);
            resultData.setMessage("添加失败");
            e.printStackTrace();
        }

        return resultData;
    }

    @Override
    public ResultData getOrderByUserId(Integer userid) {
        ResultData resultData=new ResultData();
        resultData.setData(orderMallMapper.getOrderByUserId(userid));
        return resultData;
    }

    @Override
    public ResultData deleteOrderById(Integer orderid) {
        ResultData resultData=new ResultData();
        int count= orderMallMapper.deleteOrderById(orderid);
        if(count<=0){
            resultData.setResult(1);
            resultData.setMessage("删除失败");
        }
        return resultData;
    }

    @Transactional
    @Override
    public ResultData updateOrderState(Integer orderid, Integer state,Integer ordertype) {
        ResultData resultData=new ResultData();
        if(state==5||state==6){
            List<OrderGood> orderGoods=orderGoodMapper.selectAllByOrderId(orderid);
            for(OrderGood  orderGood:orderGoods){
                bunsMapper.backByBunsIdAndNum(orderGood.getGoodid(),orderGood.getGoodnum());
            }
        }
        int count= orderMallMapper.updateOrderState(orderid,state);
        if(count<=0){
            resultData.setResult(1);
            resultData.setMessage("修改订单状态失败");
        }
        return resultData;
    }

    @Override
    public ResultData getOrderByOrderType(Integer ordertype) {
        ResultData resultData=new ResultData();
        resultData.setData(orderMallMapper.getOrderByOrderType(ordertype));
        return resultData;
    }

    @Override
    public ResultData getOrderByUserIdAndOrderType(Integer userid, Integer ordetype,Integer pageNo,Integer pageCount) {
        ResultData resultData=new ResultData();
        if (pageNo != null && pageCount != null) {
            PageHelper.startPage(pageNo, pageCount);
        }
        List<OrderMall> orderMalls=orderMallMapper.getOrderByUserIdAndOrderType(userid,ordetype);
        resultData.setData(orderMalls);
        return resultData;
    }

    @Override
    public ResultData getOrderByUserIdAndOrderType(Integer userid, Integer ordetype) {
        ResultData resultData=new ResultData();
        List<OrderMall> orderMalls=orderMallMapper.getOrderByUserIdAndOrderType(userid,ordetype);
        resultData.setData(orderMalls);
        return resultData;
    }

    @Override
    public ResultData getOrderByOrderType(Integer ordertype, Integer orderstate) {
        ResultData resultData=new ResultData();
        List<OrderMall> orderMalls=orderMallMapper.getOrderByOrderTypeAndOrderState(ordertype,orderstate);
        resultData.setData(orderMalls);
        return resultData;
    }
}
